<template>
  <div>
    <div>
      <a-table ref="table" size="small" bordered rowKey="id" :scroll="{ y: 300 }" :columns="columns" :dataSource="dataSource" :row-selection="rowSelection" :pagination="false" @change="handleTableChange">
      </a-table>
    </div>
    <resources-model ref="resourcesModel" @ok="getResourceList"></resources-model>
  </div>
</template>

<script>
import { getAction, deleteAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterObj } from '@/utils/util'
import ResourcesModel from '../../system/modules/ResourcesModel'

export default {
  name: 'ResourcesList',
  components: {
    ResourcesModel
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      rowSelection: {
        memberId: [],
        onChange: (selectedRowKeys, selectedRows) => {
        },
        onSelect: (selectedRowKeys, selected, selectedRows) => {
          console.log(selectedRowKeys)
          if (selected === true) {
            const addmember = selectedRowKeys
            this.$emit('addmemberId', addmember)
          } else {
            const deletemember = selectedRowKeys
            this.$emit('deletememberId', deletemember)
          }

          // 如果selected为true，则推送add{ }, 如果selected为false，则推送delete{ }
          // console.log(`selectedRows: ${selectedRows}`)
        },
        // 待定
        onSelectAll: (selected, selectedRows, changeRows) => {
        }
      },
      columns: [
        {
          title: '成员姓名',
          align: 'center',
          dataIndex: 'contactName'
        }
      ],
      width: 1200,
      dataSource: [],
      currSelected: [],
      title: '操作',
      layerDetail: {},
      disableSubmit: false,
      disableMixinCreated: true,
      model: {},
      threeCol: {
        label: { span: 6 },
        wrapper: { span: 24 - 6 }
      },
      menuhidden: false,
      menuusing: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      // // 分页参数
      ipagination: {
        current: 1,
        pageSize: 100,
        showTotal: (total, range) => {
          return ''
        },
        showQuickJumper: false,
        showSizeChanger: false,
        total: 0
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      url: {
        add: '/subject/scSubjectLayerInfo/layerDataAdd/',
        deleteData: '/subject/scSubjectLayerInfo/layerDataDelete/',
        listUrl: '/subject/scSubjectContactsBook/listContacts',
        listUnrelationUrl: '/subject/scSubjectContactsBook/listNoAssociatedContacts',
        deleteBatch: '/subject/scSubjectLayerInfo/layerDataDeleteBatch/'
      }
    }
  },
  mounted() { },
  methods: {
    searchQueryList() {
      this.getResourceList(1)
    },
    // 判断显示新增按钮 1和2是政府部门和外部联系人
    showOperButton(oper) {
      switch (oper) {
        case 'add':
          return this.currSelected.type === '1' || this.currSelected.type === '2'
        case 'relation':// 展示关联专家
          return this.currSelected.type === '3' && this.currSelected.name !== '未分配组'
        case 'distribution':// 展示分配组
          return this.selectedRowKeys.length > 0 && this.isNoAssociatedContacts()
      }
    },
    // 删除图层数据
    handleDeleteData(record) {
      var that = this
      const key = record.id
      deleteAction(that.url.deleteData + this.currSelected.id, { id: key }).then((resp) => {
        if (resp.success) {
          that.$message.success('删除成功!')
          this.getResourceList(this.currSelected.id)
        } else {
          that.$message.warning('删除失败!')
        }
      })
    },
    batchDel() {
      var ids = ''
      for (var a = 0; a < this.selectedRowKeys.length; a++) {
        ids += this.selectedRowKeys[a] + ','
      }
      var that = this
      this.$confirm({
        title: '确认删除',
        content: '确定要删除所选中的数据吗?',
        onOk() {
          deleteAction(that.url.deleteBatch + that.currSelected.id, { ids: ids }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.getResourceList(1)
              that.onClearSelected()
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    },
    open(record) {
      this.currSelected = record
      console.log('currSelected:', this.currSelected)
      this.getResourceList(1, record.id)
    },
    handleTableChange(pagination, filters, sorter) {
      // 分页、排序、筛选变化时触发
      console.log('ddd:', pagination)
      // TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = sorter.order == 'ascend' ? 'asc' : 'desc'
      }
      this.ipagination = pagination
      this.getResourceList()
    },
    handleAddItem(num, record) {
      if (num == 1) {
        console.log(this.currSelected)
        this.$refs.resourcesModel.add(this.currSelected)
        this.$refs.resourcesModel.title = '新增图层资源'
      } else if (num == 2) {
        this.$refs.resourcesModel.edit(record, this.currSelected)
        this.$refs.resourcesModel.title = '编辑图层资源'
        this.$refs.resourcesModel.disableSubmit = false
      } else {
        this.$refs.resourcesModel.edit(record, this.currSelected)
        this.$refs.resourcesModel.title = '图层资源详情'
        this.$refs.resourcesModel.disableSubmit = true
      }
    },
    // 判断是否是未分组
    isNoAssociatedContacts() {
      return this.currSelected.type === '3' && this.currSelected.name === '未分配组'
    },
    getResourceList(arg, id) {
      if (!id) id = this.currSelected.id
      this.dataSource = []
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = { ...this.getQueryParams(), id: id }
      console.log('params', params)
      console.log('是否是未分组：', this.isNoAssociatedContacts())
      var url = this.isNoAssociatedContacts() ? this.url.listUnrelationUrl : this.url.listUrl
      getAction(url, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
      })
    },
    getQueryParams() {
      const p = Object.assign({}, this.queryParam)
      p.pageNo = this.ipagination.current
      p.pageSize = this.ipagination.pageSize
      return filterObj(p)
    },
    searchReset() {
      this.queryParam = {}
      this.getResourceList(1)
    },
    searchQuery() {
      this.getResourceList(1)
    }
  }
}
</script>

<style scoped>
.middle-buttons {
  margin: 5px 0;
}
</style>
